import request from '@/utils/request.js'
import { getStorage } from '../utils/storage'
// 频道 - 获取 所有 频道
export const getAllChannelsAPI = () => request({
  url: '/v1_0/channels',
  method: 'GET'
})
//  -    -------  登 录(Login) --------
export const loginAPI = ({ mobile, code }) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})

//  ----------------------- 主 页 (Home/index) ---------------
// 获取 - 获取 用户选择 的频道
export const getUserChannelsAPI = () => request({
  url: '/v1_0/user/channels'
})
// 频道 更新覆盖频道
export const updataChannelsAPI = ({ channels }) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: { channels }
})
// 删除 用户指定的频道
export const removeTheChannelAPI = ({ channelId }) => request({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

//    -----  文章列表(Home/components/ArticleList.vue)--------------
// 文章 - 获取列表
export const articlesListAPI = ({ channelId, timestamp }) => {
  return request({
    url: '/v1_0/articles',
    params: {
      channel_id: channelId,
      timestamp
    }
  })
}
// 文章  反馈 不感兴趣
export const articleDislikeAPI = ({ artId }) => {
  return request({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target: artId
    }
  })
}
// 文章 - 举报
export const articleReportsAPI = ({ target, type, remark }) => {
  return request({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target: target,
      type: type,
      remark: remark
    }
  })
}

//         -------------- 搜索 (Search)  -------------
// 搜索 - 联想菜单
export const suggestListAPI = ({ keywords }) => request({
  url: '/v1_0/suggestion',
  params: {
    q: keywords
  }
})
// 搜索 - 搜索结果列表
export const searchResultAPI = ({ page = 1, perPage = 10, q }) => request({
  url: '/v1_0/search',
  params: {
    page,
    perPage,
    q
  }
})

//     ---------------  文章 页面 (ArticleDetail) ------------
// 获取 文章详情
export const detailAPI = ({ id }) => request({
  url: `/v1_0/articles/${id}`
})
// 用户 - 关注
export const userFollowedAPI = ({ userId }) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: userId
  }
})
// 用户 - 取消关注
export const userUnFollowedAPI = ({ userId }) => request({
  url: `/v1_0/user/followings/${userId}`,
  method: 'DELETE'
})
// 文章点赞
export const articleLikeAPI = ({ artId }) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})
// 取消文章点赞
export const articleDisLikeAPI = ({ artId }) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

//  -------- 文章评论(ArticleDetail/CommentList.vue)--------------
// 获取评论
export const commentsListAPI = ({ id, offset = null, limit = 10 }) => request({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: id,
    offset,
    limit
  }
})
// 对评论点赞
export const commentLikingAPi = ({ id }) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: id
  }
})
// 取消对评论点赞
export const commentDisLikingAPi = ({ id }) => request({
  url: `/v1_0/comment/likings/${id}`,
  method: 'DELETE'
})
// 发布评论
export const commentsSendAPI = ({ id, content, artId }) => {
  // axios中,data请求体传参,如果值为null不会忽略这个参数 ,
  //  只有上面那个 params遇到null会忽略
  const obj = {
    target: id,
    content
  }
  if (artId !== null) {
    obj.art_id = artId
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}
// --------------  用户 - 获取基本信息(User)  -------------------
export const getUserInfoAPI = () => request({
  url: '/v1_0/user'
})
// 用户- 个人资料(就为了获取生日) (User/UserEdit.vue)
export const userProfileAPI = () => request({
  url: '/v1_0/user/profile'
})
// 用户 - 更新头像
export const uesrPhotoAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd
})
// 用户 - 更新基本资料
export const updateUserProfileAPI = (dataObj) => {
  // 可不写  ---------
  const obj = {
    name: '', // 昵称
    gender: 0, // 性别
    birthday: '', // 生日
    intro: '' // 个人介绍
  }
  for (const key in obj) {
    if (dataObj[key] === undefined) {
      delete obj[key]
    } else {
      obj[key] = dataObj[key]
    }
  } // ---------
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}
// 优化 更新 token
export const getNewTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer ' + getStorage('refresh_token')
  }
})
